package com.aaa.operations.controller;


import com.aaa.common.entity.ResultData;
import com.aaa.common.entity.Room;
import com.aaa.common.entity.RoomUser;
import com.aaa.operations.service.RoomService;
import com.aaa.operations.service.RoomUserService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-07-29
 */
@RestController
@RequestMapping("/roomUser")
public class RoomUserController {
    @Autowired
    private RoomUserService roomUserService;
    @Autowired
    private RoomService roomService;


    //入住
    @RequestMapping("/editRoom")
    public ResultData editRoom(@RequestBody RoomUser roomUser){

        if(roomUser.getUserId()==null){
            return new ResultData(false, "请选择正确员工", null);
        }

        try {
                boolean insert = roomUserService.insert(roomUser);
                //boolean b = roomUserService.updateById(roomUser);
                if (insert) {
                    Room room = new Room();
                    room.setRoomId(roomUser.getRoomId());
                    room.setRoomState(1);
                    SimpleDateFormat dateFormat = new SimpleDateFormat(" yyyy-MM-dd ");
                    String currentDate =   dateFormat.format( new Date() );

                    room.setInroomTime(currentDate);
                    roomService.updateById(room);
                    return new ResultData(true, "入住成功", null);
                } else {
                    return new ResultData(false, "入住失败", null);
                }
            }catch (Exception e){
                return new ResultData(false, "入住失败", null);

            }

    }

    //退宿
    @RequestMapping("/outRoom")
    public ResultData outRoom(@RequestBody RoomUser roomUser){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("room_id",roomUser.getRoomId());
        System.out.println("---------------"+roomUser);
        boolean delete = roomUserService.delete(wrapper);
        if(delete){
            if(roomUser.getRoomId()!=null && !roomUser.getRoomId().equals("")){
                Room room = new Room();
                room.setRoomId(roomUser.getRoomId());
                room.setRoomState(0);
                room.setInroomTime("");
                System.out.println(room);
                boolean b = roomService.updateById(room);
                if(b){
                    return new ResultData(true,"退宿成功",null);
                }else {
                    return new ResultData(false,"退宿失败",null);
                }
            }else {
                return new ResultData(false,"退宿失败",null);
            }


        }else {
            return new ResultData(false,"退宿失败",null);
        }
    }
    //更换寝室chengeRoom
    @RequestMapping("/chengeRoom")
    public ResultData chengRoom(@RequestParam("roomNo")Integer roomNo, @RequestParam("roomStorey") Integer roomStorey,
                                @RequestParam("userId")Integer userId, @RequestParam("roomId")Integer roomId){

        System.out.println("修改之前的roomId"+roomId);
        System.out.println("修改之后的寝室号"+roomNo);
        System.out.println("修改之后的楼层号"+roomStorey);
        System.out.println("员工ID"+userId);




        //根据之前的roomId把room改为无人入住状态
        Room room = new Room();
        room.setRoomId(roomId);
        room.setRoomState(0);
        room.setInroomTime("");
        boolean b = roomService.updateById(room);
        //如果修改成功，则根据员工ID删除之前的宿舍和员工中间表
        if(b){
            Wrapper wrapper2 = new EntityWrapper();
            wrapper2.eq("user_id",userId);
            boolean delete = roomUserService.delete(wrapper2);
            //如果删除成功，则此员工等同于没有宿舍，开始重新分配宿舍，重新加中间表数据
            if(delete){
                //先根据宿舍楼层和宿舍号查到宿舍,拿到要入住的宿舍信息
                Wrapper wrapper3 = new EntityWrapper();
                wrapper3.eq("room_storey",roomStorey);
                wrapper3.eq("room_no",roomNo);
                List<Room> list = roomService.selectList(wrapper3);
                Room room1 = list.get(0);
                System.out.println("修改之后的room信息为"+room1);
                //拿到宿舍之后，把这个宿舍状态改为入住状态
                room1.setRoomState(1);
                SimpleDateFormat dateFormat = new SimpleDateFormat(" yyyy-MM-dd ");
                String currentDate =   dateFormat.format( new Date() );
                room1.setInroomTime(currentDate);
                boolean b1 = roomService.updateById(room1);
                //如果修改成功，则为中间表新增数据
                if(b1){
                    RoomUser roomUser = new RoomUser();
                    roomUser.setRoomId(room1.getRoomId());
                    roomUser.setUserId(userId);
                    boolean insert = roomUserService.insert(roomUser);
                    //如果新增成功则成功返回
                    if(insert){
                        return new ResultData(true,"更换成功",null);

                    }
                }
            }
        }else {
            return new ResultData(false,"更换失败",null);
        }

        /*if(delete){


            return new ResultData(true,"更换成功",null);

        }else {

            return new ResultData(false,"更换失败",null);
        }*/
        return null;
    }

}

